import json
import sys
from pathlib import Path
from os.path import dirname

path = dirname(str(Path(__file__).resolve().parents[2]))
if path not in sys.path:
    sys.path.append(path)

from xkw_data_query.bapi.bapi_call import BapiCaller

from xkw_data_query.config_parse import get_bapi_config

config = get_bapi_config("mdm")
caller = BapiCaller(True, config["app_id"], config["app_secret"])


def fetch_catalog_by_ids(catalog_ids: list):
    """
    获取目录信息
    返回值:
    [
        {
            "id": 1,
            "name": "目录1",
            "textbookId": 1
        }
    ]
    """
    catalog_ids = [str(id) for id in catalog_ids]
    catalog_ids_str = ",".join(catalog_ids)
    res = caller.get(f"/textbook-catalog/items?catalogids={catalog_ids_str}")
    if res is None:
        return []
    res_objs = json.loads(res)
    return res_objs


def fetch_textbook_by_ids(textbook_ids: list):
    """
    获取教材信息
    返回值:
    [
        {'courseId': 33, 'createTime': '2019-03-18T10:29:36.000', 'gradeId': 10, 'id': 4665, 'inUse': True, 'name': '高中历史统编版（2019）必修中外历史纲要上', 'ordinal': 1, 'term': 'LAST', 'versionId': 728, 'volume': '必修中外历史纲要上'}
    ]
    """
    textbook_ids = [str(id) for id in textbook_ids]
    textbook_ids_str = ",".join(textbook_ids)
    res = caller.post(f"/textbooks", {"ids": textbook_ids_str})
    if res is None:
        return []
    res_objs = json.loads(res)
    return res_objs


def fetch_knowledge_point_by_id(knowledge_point_id):
    """
    获取知识点信息
    返回值:
    {'courseId': 33, 'createTime': '2019-08-15T17:19:49.000', 'depth': 0, 'forLite': True, 'hasCard': True, 'id': 70861, 'name': '商品的世界性流动', 'ordinal': 2, 'parentId': 70796, 'tag': 'STANDARD', 'tagId': 19364, 'type': 'KNOWLEDGE_POINT', 'updateTime': '2023-02-14T13:51:04.000'}
    """
    res = caller.get(f"/knowledge-points/{knowledge_point_id}")
    if res is None:
        return None
    return json.loads(res)


def fetch_all_areas():
    """
    获取所有地区
    返回值:
    [
        {
            "id": "820100",
            "code": "820100",
            "name": "澳门特别行政区",
            "inUse": True
        }
    ]
    """
    res = caller.get(f"/areas")
    if res is None:
        return []
    res_objs = json.loads(res)
    return res_objs

def fetch_all_grades():
    """
    获取所有年级
    返回值:
    [
        {
            "id": 1,
            "name": "一年级",
            "ordinal": 1
        }
    ]
    """
    res = caller.get(f"/grades")
    if res is None:
        return []
    res_objs = json.loads(res)
    return res_objs

def fetch_all_textbook_versions():
    """
    获取所有教材版本
    返回值:
    [       
        {
            "courseId": 1,
            "description": "1",
            "familyId": 1,
            "id": 521,
            "inUse": false,
            "name": "人教版（1990s）",
            "ordinal": 1,
            "year": 1990
        }
    ]
    """
    res = caller.get(f"/textbook-versions") 
    if res is None:
        return []
    res_objs = json.loads(res)
    return res_objs

def fetch_all_courses():
    """
    获取所有课程
    返回值:
    [       
        {
            "areaIds": "1",
            "courseType": 35477,
            "createTime": "2019-02-27T10:38:19.000",
            "id": 1,
            "name": "小学语文",
            "ordinal": 1,
            "stage": "PRIMARY",
            "subjectId": 1,
            "updateTime": "2019-02-27T10:38:19.000"
        }
    ]
    """
    res = caller.get(f"/courses") 
    if res is None:
        return []
    res_objs = json.loads(res)
    return res_objs
    
if __name__ == "__main__":
    res = fetch_all_textbook_versions()
    print(res)

    